package com.example.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Service;

import com.example.domain.Book;

@Service

//@ManagedResource(objectName="annojmx:myjao=AnnotationObject",description="mytest")
@ManagedResource
public class MapService {
	
	/**
	 * map以及jmx 监控
	 * */
	private Map<String,Book> b1=new HashMap<String ,Book>();
	@PostConstruct
	public void init(){
		Book value=new Book();
		value.setId("123233");
		value.setName("1111");
		b1.put("1", value);
	}
	//@ManagedAttribute
	@ManagedOperation(description="获得value")
	@ManagedOperationParameters({
		@ManagedOperationParameter(name="key", description="主键")
		//,@ManagedOperationParameter(name="key", description="主键")
	})
	public String getBykey(String key){
		if(b1.containsKey(key)){
			return b1.get(key).getName();
		}
		return null;
	}
	
	/**
	 * 把方法标记为JMX的操作	ManagedOperation	@ManagedOperation
	 * 把getter或setter标记为JMX的半个属性	ManagedAttribute	@ManagedAttribute	方法（仅 getters 和 setters）
	 * 具体见
	 * http://www.blogjava.net/xmatthew/archive/2008/11/26/242889.html
	 * */
	@ManagedOperation()
	@ManagedOperationParameter(name="value", description = "修改key")
	public void setValue(String value){
		Book b=b1.get("1");
		b.setName(value);

		b1.put("1", b);
	}

}
